package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.icss.beans.AppercetionVO;
import com.icss.utils.RecordSet;

/**
 * @author 韦锦良
 *　类的作用：学习心得逻辑处理类
 * 最后修改时间：2010-3-22　下午12:44:21
 * 最后的改动内容：初始版本
 */
public class AppercetionDao {

	//格式化字符串
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private ResultSet rs = null;

	private Dao dao = new Dao();

	//通过用户的id获得所有的心得的标题
	public ArrayList<AppercetionVO> getAllTitleByUid(String uid) {

		String sql = "select * from Appercetion where uid=" + uid + " order by publishTime desc";

		ArrayList<AppercetionVO> vos = null;

		rs = dao.query(sql);

		vos = getVOFromResultSet(rs);

		close();

		return vos;

	}

	//通过心得编号获得心得的相关所有信息
	public AppercetionVO getAppercetionVOById(String appercetionID) {

		AppercetionVO vo = null;

		String sql = "select * from Appercetion where appercetionID=" + appercetionID;

		rs = dao.query(sql);

		vo = getVOFromResultSet(rs).get(0);

		close();

		return vo;

	}

	//将ResultSet封装到vo类里面去
	public ArrayList<AppercetionVO> getVOFromResultSet(ResultSet rs) {

		ArrayList<AppercetionVO> vos = new ArrayList<AppercetionVO>();

		try {
			while (rs.next()) {
				AppercetionVO vo = new AppercetionVO();
				vo.setAppercetionId(rs.getInt("appercetionID"));
				vo.setUid(rs.getInt("uid"));
				vo.setAppercetionBody(rs.getString("appercetionBody"));
				vo.setAppercetionTitle(rs.getString("appercetionTitle"));
				vo.setPublisher(rs.getString("publisher"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
				vo.setVisiable(rs.getByte("isPublic"));
				vos.add(vo);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close();
		}

		return vos;

	}

	//将RecordSet里面的数据封装到vo类里面去
	public ArrayList<AppercetionVO> getVOFromRecordSet(RecordSet rs) {

		ArrayList<AppercetionVO> vos = new ArrayList<AppercetionVO>();

		while (rs.next()) {
			AppercetionVO vo = new AppercetionVO();
			vo.setAppercetionId((int) rs.getNumber("APPERCETIONID"));
			vo.setUid((int) rs.getNumber("UID"));
			vo.setAppercetionBody(rs.getString("APPERCETIONBODY"));
			vo.setAppercetionTitle(rs.getString("APPERCETIONTITLE"));
			vo.setPublisher(rs.getString("PUBLISHER"));
			vo.setPublishTime(sdf.format(rs.getTimestamp("PUBLISHTIME")));
			vo.setVisiable(rs.getByte("ISPUBLIC"));
			vos.add(vo);
		}
		close();
		return vos;

	}

	//获得符合前面界面显示条件的心得
	public ArrayList<AppercetionVO> getAppercetionsForFoward(int PageSize, int currentPage) {

		String strSQL = "select * from Appercetion where isPublic=1 order by publishTime desc";
		ArrayList<AppercetionVO> vos = null;
		vos = getVOFromRecordSet(dao.queryPager(strSQL, PageSize, currentPage));
		dao.close();
		return vos;

	}

	//关闭连接
	public void close() {

		try {
			if (null != rs) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		dao.close();

	}

	//执行sql语句 
	public boolean execute(String sql) {
		return dao.execute(sql);
	}

	//根据给定的每页显示条数、要获取第几页、以及搜索的类型、以及类型的值获得相关的数据
	public RecordSet getInfoForAdmin(int pageSize, int currentPage, String type, String typeArg) {
		RecordSet set = null;
		StringBuffer strSQL = null;
		if ("".equals(typeArg) && type != null) {
			return set;
		}
		if ("byName".equals(type)) {

			strSQL = new StringBuffer(
					"select appercetionId, uid, publisher, appercetionTitle, publishTime, isPublic from Appercetion where publisher like '%");
			strSQL.append(typeArg).append("%' order by publishTime desc");
		} else if ("byDate".equals(type)) {

			strSQL = new StringBuffer(
					"select appercetionId, uid, publisher, appercetionTitle, publishTime, isPublic from Appercetion  where convert(varchar(20),publishTime,120) like '%");
			strSQL.append(typeArg).append("%' order by publishTime desc ");
		} else if (type == null) {

			strSQL = new StringBuffer(
					"select appercetionId, uid, publisher, appercetionTitle, publishTime, isPublic from Appercetion order by publishTime desc");
		}

		set = dao.queryPager(strSQL.toString(), pageSize, currentPage);
		return set;
	}

}
